package io.finer.erp.jeecg.stock.service.impl;

import io.finer.erp.jeecg.bas.entity.BasMaterialCategory;
import io.finer.erp.jeecg.bas.service.IBasMaterialCategoryService;
import io.finer.erp.jeecg.stock.entity.StkProcessingOrder;
import io.finer.erp.jeecg.stock.entity.StkProcessingRecord;
import io.finer.erp.jeecg.stock.mapper.StkProcessingRecordMapper;
import io.finer.erp.jeecg.stock.mapper.StkProcessingOrderMapper;
import io.finer.erp.jeecg.stock.service.IStkProcessingOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;

import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Collection;
import java.util.Iterator;

/**
 * @Description: 代加工单
 * @Author: jeecg-boot
 * @Date:   2021-04-17
 * @Version: V1.0
 */
@Service
public class StkProcessingOrderServiceImpl extends ServiceImpl<StkProcessingOrderMapper, StkProcessingOrder> implements IStkProcessingOrderService {

	@Autowired
	private StkProcessingOrderMapper stkProcessingOrderMapper;
	@Autowired
	private StkProcessingRecordMapper stkProcessingRecordMapper;
	@Autowired
	private IBasMaterialCategoryService categoryService;
	
	@Override
	@Transactional
	public void saveMain(StkProcessingOrder stkProcessingOrder, List<StkProcessingRecord> stkProcessingRecordList) {
//		BigDecimal totalAmount = BigDecimal.ZERO;
//		if(CollUtil.isNotEmpty(stkProcessingRecordList)) {
//			for (Iterator<StkProcessingRecord> iterator = stkProcessingRecordList.iterator(); iterator.hasNext();) {
//				StkProcessingRecord record = iterator.next();
//				if(null != record && null != record.getProcessingAmount()) {
//					totalAmount = totalAmount.add(record.getProcessingAmount());
//				}
//			}
//		}
//		stkProcessingOrder.setTotalAmount(totalAmount);
		stkProcessingOrderMapper.insert(stkProcessingOrder);
		if(stkProcessingRecordList!=null && stkProcessingRecordList.size()>0) {
			for(StkProcessingRecord entity:stkProcessingRecordList) {
				//外键设置
				entity.setOrderId(stkProcessingOrder.getId());
				stkProcessingRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(StkProcessingOrder stkProcessingOrder,List<StkProcessingRecord> stkProcessingRecordList) {
//		BigDecimal totalAmount = BigDecimal.ZERO;
//		if(CollUtil.isNotEmpty(stkProcessingRecordList)) {
//			for (Iterator<StkProcessingRecord> iterator = stkProcessingRecordList.iterator(); iterator.hasNext();) {
//				StkProcessingRecord record = iterator.next();
//				if(null != record && null != record.getProcessingAmount()) {
//					totalAmount = totalAmount.add(record.getProcessingAmount());
//				}
//			}
//		}
//		stkProcessingOrder.setTotalAmount(totalAmount);
		stkProcessingOrderMapper.updateById(stkProcessingOrder);
		
		//1.先删除子表数据
		stkProcessingRecordMapper.deleteByMainId(stkProcessingOrder.getId());
		
		//2.子表数据重新插入
		if(stkProcessingRecordList!=null && stkProcessingRecordList.size()>0) {
			for(StkProcessingRecord entity:stkProcessingRecordList) {
				//外键设置
				entity.setOrderId(stkProcessingOrder.getId());
				stkProcessingRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		stkProcessingRecordMapper.deleteByMainId(id);
		stkProcessingOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stkProcessingRecordMapper.deleteByMainId(id.toString());
			stkProcessingOrderMapper.deleteById(id);
		}
	}

	@Override
	public Result<?> processingOutGetOrder() {
		List<StkProcessingOrder> list = this.lambdaQuery().in(StkProcessingOrder::getOrderStatus, CollUtil.newArrayList("1", "2")).last(" limit 200 ").orderByDesc(StkProcessingOrder::getCreateTime).list();
		return Result.ok(list);
	}

	@Override
	public Result<?> processingInGetOrder() {
		List<StkProcessingOrder> list = this.lambdaQuery().in(StkProcessingOrder::getOrderStatus, CollUtil.newArrayList("3", "4")).last(" limit 200 ").orderByDesc(StkProcessingOrder::getCreateTime).list();
		return Result.ok(list);
	}

	@Override
	public Result<?> getProcessingCategory(String processingId) {
		List<StkProcessingRecord> records = stkProcessingRecordMapper.selectByMainId(processingId);
		if(CollUtil.isEmpty(records)) {
			return Result.ok(Lists.newArrayList());
		}
		List<String> categoryIds = Lists.newArrayList();
		categoryIds.add(UUID.randomUUID().toString());
		for (StkProcessingRecord record : records) {
			String categoryId = record.getInCategoryId();
			if(null != categoryId) {
				categoryIds.add(categoryId);
			}
		}
		List<BasMaterialCategory> list = categoryService.listByIds(categoryIds);
		if(CollUtil.isEmpty(list)) {
			return Result.ok(Lists.newArrayList());
		}
		
		return Result.ok(list);
	}
	
}
